<?php
namespace app\api\controller\wanlshop;

use app\common\controller\Api;

/**
 * WanlShop活动接口
 */
class Activity extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
	

	/**
     * 活动报名
     *
     * @ApiSummary  (WanlShop 活动报名)
     * @ApiMethod   (POST)
	 * 
	 * @param string $name 活动名称
	 */
    public function application()
    {
    	$activityModel = model('app\api\model\wanlshop\Activity');
    	$data = [];
    	$data['name'] = input('name');
    	if (!$data['name']) {
			$this->error(__('请输入活动名称'));
		}
    	$data['dec'] = input('dec');
    	$user_id = $this->auth->id;
		$shop = model('app\api\model\wanlshop\Shop')
			->where(['user_id' => $user_id])
			->find();
		$data['shopid'] = $shop['id'];
		$data['productids'] = input('productids');
		if (!$data['productids']) {
			$this->error(__('请选择产品'));
		}
		$data['goodnum'] = count(json_decode($data['productids'],true));
		$data['start'] = input('start');
		$data['end'] = input('end');
		$data['status'] = 0;
		if ($activityModel->insert($data)) {
			$this->success('报名成功');
		}else{
			$this->error('报名失败');
		}
    }
    /**
     * 获取指定活动列表
     *
     * @ApiSummary  (WanlShop 获取活动列表)
     * @ApiMethod   (POST)
	 * 
	 * @param string $list_rows 每页数量
	 */
    public function getList()
    {

		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isPost()) {
			$type = $this->request->post('type');
			$user_id = $this->auth->id;
    		$shop = model('app\api\model\wanlshop\Shop')
				->where(['user_id' => $user_id])
				->find();
			$where['status'] = 1;
			$where['shopid'] = $shop['id'];
			$config = get_addon_config('wanlshop');
			$data = model('app\api\model\wanlshop\Activity')
				->where($where)
				->order('createtime desc')
				->paginate();
			
			$this->success('返回成功', $data);
		}
		$this->error(__('非法请求'));
        
    }
    
	
    /**
     * 获取活动详情
     *
     * @ApiSummary  (WanlShop 获取活动详情)
     * @ApiMethod   (POST)
     * 
	 * @param string $id 活动ID
     */
    public function details()
    {
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		$id = $this->request->get('id');
		$id ? $id : ($this->error(__('Invalid parameters')));
		$row = model('app\api\model\wanlshop\Activity')
			->where(['id' => $id])
			->find();
		// 1.0.5升级
		if(!$row){
			$this->error(__('没有找到任何内容'));
		}
		// 点击 +1
		$row->setInc('view');
		$this->success('返回成功', $row);
    }
	
}
